<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Login page
 *
 * The login page will both display the login page, and process login and logout
 * request.
 *
 */

/**
 * Load required files
 */
// Init values
$username = null;
$password = null;
$gw_address = null;
$gw_port = null;
$gw_id = null;
$mac = null;
$logout = null;
$url = null;
$form_request = null;
$automatic_login = false;
$valid_username = "utente";
$valid_password = "password";
$valid_token = "a10548a1308743618ccfbfe01eb436ee";

/*
 * General request parameter processing section
 */

//print "VARIABILI\n";

if (isset($_POST["form_request"])) {
    $form_request = $_POST['form_request'];
    //print "gw_address $gw_address\n";
    
    if (isset($_POST["gw_address"])) {
        $gw_address = $_POST['gw_address'];
        //print "gw_address $gw_address\n";
    }

    if (isset($_POST["gw_port"])) {
        $gw_port = $_POST['gw_port'];
        //print "gw_port $gw_port\n";
    }

    if (isset($_POST["gw_id"])) {
        $gw_id = $_POST['gw_id'];
        //print "gw_id $gw_id\n";
    }

    if (isset($_POST["logout"])) {
        $logout = $_POST['logout'];
        //print "logout $logout\n";
    }

    if (isset($_POST["mac"])) {
        $mac = $_POST['mac'];
        //print "mac $mac\n";
    }
    
    if (isset($_POST["url"])) {
        $url = $_POST['url'];
        //print "mac $mac\n";
    }
    
}
else
{

    if (isset($_REQUEST["gw_address"])) {
        $gw_address = $_REQUEST['gw_address'];
        //print "gw_address $gw_address\n";
    }

    if (isset($_REQUEST["gw_port"])) {
        $gw_port = $_REQUEST['gw_port'];
        //print "gw_port $gw_port\n";
    }

    if (isset($_REQUEST["gw_id"])) {
        $gw_id = $_REQUEST['gw_id'];
        //print "gw_id $gw_id\n";
    }

    if (isset($_REQUEST["logout"])) {
        $logout = $_REQUEST['logout'];
        //print "logout $logout\n";
    }

    if (isset($_REQUEST["mac"])) {
        $mac = $_REQUEST['mac'];
        //print "mac $mac\n";
    }

    /*
     * Store original URL typed by user
     */
    if (!empty($_REQUEST['url'])) {
        $url = $_REQUEST['url'];
        //print "url $url\n";
    }
}

$html  = "";
if ($form_request != null)
{

    if ($form_request == "login")
    {
        if (isset($_POST["username"])) {
            $username = $_POST['username'];
        }
        if (isset($_POST["password"])) {
            $password = $_POST['password'];
        }
        
        if ($username != NULL && $username == $valid_username && $password != NULL && $password == $valid_password)
        {
            // Genero token casuale e lo salvo
            $token = "$mac" . time();
            $fp = fopen('../status.php' , 'w');
	    fputs($fp, "<?\r\n");
            fputs($fp, "\t\$status['token'] = \"" . $token . "\";\r\n");
            if ($url != null && $url != "")
            {
                fputs($fp, "\t\$status['url'] = \"" . $url . "\";\r\n");
            }
            else
            {
                fputs($fp, "\t\$status['url'] = \"\";\r\n");
            }
            fputs($fp, "\t\$status['gw_address'] = \"" . $gw_address . "\";\r\n");
            fputs($fp, "\t\$status['gw_port'] = \"" . $gw_port . "\";\r\n");
            fputs($fp, "\r\n?>\r\n");
            fclose($fp);
            
            header("Location: http://" . $gw_address . ":" . $gw_port . "/wifidog/auth?token=" . $token);
        }
        else
        {
            $html .= "<HTML>\n";
            $html .= "   <HEAD>\n";
            $html .= "      <TITLE>\n";
            $html .= "         Captive Portal \n";
            $html .= "      </TITLE>\n";
            $html .= "   </HEAD>\n";
            $html .= "<BODY>\n";
            $html .= "   <H1>:-( Username and/or password are wrong.</H1>\n";
            $html .= "   <a href='index.php?gw_address=${gw_address}&gw_port=${gw_port}&gw_id=${gw_id}&mac=${mac}'>Login again...</a>";
            $html .= "</BODY>\n";
            $html .= "</HTML>\n";
        }
    }
    else if ($form_request == "logout")
    {
        require('../status.php');
	$t = $status['token'];
	/*$fp = fopen('../status.php' , 'w');
        fputs($fp, "<?\r\n");
        fputs($fp, "\t\$status['token'] = \"\";\r\n");
        fputs($fp, "\t\$status['url'] = \"\";\r\n");
        fputs($fp, "\r\n?>\r\n");
        fclose($fp);*/
        header("Location: http://" . $gw_address . ":" . $gw_port . "/wifidog/auth?logout=1&token=" . $t);
    }
}
else
{
    if ( $automatic_login == true )
    {
        // Genero token casuale e lo salvo
        $token = "AA:BB:CC:DD:EE:FF0000000000";
        $fp = fopen('../status.php' , 'w');
        fputs($fp, "<?\r\n");
        fputs($fp, "\t\$status['token'] = \"" . $token . "\";\r\n");
        fputs($fp, "\t\$status['url'] = \"\";\r\n");
        fputs($fp, "\t\$status['gw_address'] = \"" . $gw_address . "\";\r\n");
        fputs($fp, "\t\$status['gw_port'] = \"" . $gw_port . "\";\r\n");
        fputs($fp, "\r\n?>\r\n");
        fclose($fp);    
        header("Location: http://" . $gw_address . ":" . $gw_port . "/wifidog/auth?token=" . $token);
    }
    else
    {
        $html .= "<HTML>\n";
        $html .= "   <HEAD>\n";
        $html .= "      <TITLE>\n";
        $html .= "         Captive Portal \n";
        $html .= "      </TITLE>\n";
        $html .= "   </HEAD>\n";
        $html .= "<BODY>\n";
        $html .= "   <H1>Login Form</H1>\n";
        $html .= "   <form name='login_form'  id='login_form' action='index.php' method='post'>\n";
        $html .= "      <input type='hidden' name='form_request' value='login'>\n";
        if ($gw_address != null)
            $html .= "      <input type='hidden' name='gw_address' value='{$gw_address}'>\n";
        if ($gw_port != null)
            $html .= "      <input type='hidden' name='gw_port' value='{$gw_port}'>\n";
        if ($gw_id != null)
            $html .= "      <input type='hidden' name='gw_id' value='{$gw_id}'>\n";
        if ($url != null)
            $html .= "      <input type='hidden' name='url' value='{$url}'>\n";        
        if ($mac != null)
            $html .= "      <input type='hidden' name='mac' value='{$mac}'>\n";
        $html .= "      USERNAME: <input type='text'     name='username'><br />\n";
        $html .= "      PASSWORD: <input type='password' name='password'><br /><br />\n";
        $html .= "      <input type='submit' value='Login' /><br />\n";
        $html .= "   </form>\n";
        $html .= "</BODY>\n";
        $html .= "</HTML>\n";
    }
}
print "$html";
?>
